import { App, mall, navigate, toast } from '../../../library/api/index'

Page({
  data: {
    navMe: 'on',
    tip: 'blank',
    tipData: {},
    status: 0,
    status_message: '您还没有订单',
    redeem_status_code: 0,
    redeem_status_message: '',
    redeem: {},
    validdate: null,
    invaliddate: null,
    swipers: {
      p70: ['https://r.epclass.meadjohnson.joypaw.com/p/v2/p0.png', 'https://r.epclass.meadjohnson.joypaw.com/p/v2/p70.png'],
      p90: ['https://r.epclass.meadjohnson.joypaw.com/p/v2/p0.png', 'https://r.epclass.meadjohnson.joypaw.com/p/v2/p90.png']
    },
    swiper: []
  },
  onLoad: function(e) {
    App.jice.onLoad(e)
  },
  onReady: function() {
    this.isNua()
    App.jice.track('page', {
      pagepath: '/pages/me/order/index',
      pageTitle: '个人中心-订单-首页',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '个人中心-订单-首页'
    })
  },
  onShow: function() {
    this.getUserOrdersStatus()
    this.checkUserHasRedeem()
    App.jice.track('appshow')
  },
  isNua: function() {
    mall.isNua(r => {
      if(r.status) {
        this.setData({
          swiper: this.data.swipers.p70
        })
      } else {
        this.setData({
          swiper: this.data.swipers.p90
        })
      }
    }, r => {
      this.setData({
        swiper: this.data.swipers.p90
      })
    })
  },
  getUserOrdersStatus: function() {
    toast.show()
    mall.getOrdersStatus(r => {
      toast.close()
      if(r.code >= 200 && r.code < 300) {
        this.setData({
          status: r.code,
          status_message: r.data,
        })
      } else {
        this.showErrorTip(r.data)
      }
    }, r => {
      console.log(r)
      this.showErrorTip('系统错误，请稍后再试')
    })
  },
  listOrders: function() {
    if(this.data.status == 202 || this.data.status == 0) {
      this.setData({
        tip: 'go_mall_index',
        tipData: '您还没有订单，请到待产包商城选购商品哦'
      })
      return
    }
    navigate.to(`/pages/me/order/list/index`)
  },
  showQuestion: function() {
    console.log('showQuestion')
    this.setData({
      tip: 'question',
      tipData: {}
    })
  },
  checkUserHasRedeem: function() {
    mall.hasRedeem(r => {
      if(r.code >= 200 && r.code < 300) {
        this.setData({
          redeem_status_code: r.code,
          redeem_status_message: r.data,
          redeem: r.result
        })
      } else {
        // 禁止是否有提货券的错误输出
        // this.showErrorTip(r.data)
      }
    }, r => {
      console.log(r)
      this.showErrorTip('系统错误，请稍后再试')
    })
  },
  showRedeemByIsNotValid: function() {
    this.setData({
      tip: 'redeem_is_not_valid',
      tipData: {
        code: this.data.redeem.code,
        name: this.data.redeem.products.name,
        img: this.data.redeem.products.mainimg
      }
    })
  },
  showRedeemByIsValid: function() {
    this.setData({
      tip: 'redeem',
      tipData: {
        code: this.data.redeem.code,
        name: this.data.redeem.products.name,
        validdate: this.data.redeem.validdate,
        invaliddate: this.data.redeem.invaliddate,
        img: this.data.redeem.products.mainimg,
      }
    })
  },
  showRedeemByIsUsed: function() {
    this.setData({
      tip: 'redeem_is_used',
      tipData: {
        code: this.data.redeem.code,
        name: this.data.redeem.products.name,
        validdate: this.data.redeem.validdate,
        invaliddate: this.data.redeem.invaliddate,
        img: this.data.redeem.products.mainimg,
      }
    })
  },
  pickup: function() {
    mall.pickupRedeem(r => {
      if(r.code > 500) {
        this.showErrorTip(r.data)
        return
      }
      if(r.code == 200) {
        this.showErrorTip('提货成功，请到【个人中心】-【全部订单】查看订单状态。')
        this.checkUserHasRedeem()
      }
    }, r => {
      console.log(r)
      this.showErrorTip('系统错误，请稍后再试')
    })
  },
  showErrorTip: function(message) {
    this.setData({
      tip: 'error_tip',
      tipData: message
    })
  },
  goMallIndex() {
    navigate.redirect('/pages/mall/index')
  },
  closeTip: function() {
    this.setData({
      tip: 'blank',
      tipData: {}
    })
  }
})